#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <curses.h>
#include <stdlib.h>

typedef struct {
    char * name;
   } person_t;

 const char *dr = "/home/mirus/src/dev/scripts";
 
 int namecmp(const void* a, const void* b);
 
 int main(void)
 {
    person_t *base;
    //person_t base[56];
    
    base = calloc(1, (56 * sizeof(person_t)));
     
    DIR *dir;
    struct dirent *ent;
    struct stat sb;
     if ((dir = opendir(dr)) == NULL) {
        fprintf(stderr, "Failed to open %s: %s\n", dr, strerror(errno));
        return 1;
    }
    if (chdir(dr) == -1) {
        fprintf(stderr, "Failed to chdir %s: %s\n", dr, strerror(errno));
        return 1;
    }
     int l=0;
    while ((ent = readdir(dir))) {
        if (stat(ent->d_name, &sb)) {
            fprintf(stderr, "Stat failed %s: %s\n", ent->d_name, strerror(errno));
            return 1;
        }
        if ((sb.st_mode & S_IFMT) == S_IFREG) {
            base[l].name = (char *)calloc(1, strlen(ent->d_name)+1);
            sprintf(base[l].name, "%s", ent->d_name);
            printf("%s\n", base[l].name);
            l++;
        }
    }
    closedir(dir);
 
    
    
    qsort(base, 56, sizeof(person_t), namecmp);
    int i;
    for(i = 0 ; i < 56 ; i++)  printf("%s\n", base[i].name);
    printf("\n%d\n", (int)sizeof(person_t));
    for(i = 0 ; i < 56 ; i++) free(base[i].name);
    free(base);

    return 0;
 
 }
 
 int namecmp(const void* a, const void* b)
 {
     person_t *pa = a;
     person_t *pb = b;
     return strcmp(pa->name, pb->name);
 }
 
 